//
//  RechargeDetailsViewController.m
//  CRSCMOA
//
//  Created by goldnut on 16/9/1.
//  Copyright © 2016年 王文晓. All rights reserved.
//

#import "RechargeDetailsViewController.h"
#import "RechargeDetailsCell.h"

@interface RechargeDetailsViewController ()<UITableViewDelegate,UITableViewDataSource,UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSString *cardBalance;
@property (nonatomic, strong)  NSString *dateStr;
@property (weak, nonatomic) IBOutlet UIView *startView;
@property (weak, nonatomic) IBOutlet UILabel *startLabel;
@property (weak, nonatomic) IBOutlet UIView *endView;
@property (weak, nonatomic) IBOutlet UILabel *endLabel;
- (IBAction)queryBtn:(UIButton *)sender;
- (IBAction)startTap:(UITapGestureRecognizer *)sender;
- (IBAction)endTap:(UITapGestureRecognizer *)sender;

@end


@implementation RechargeDetailsViewController
{
    
    UIView *dateView ;
    UIDatePicker *datePicker;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

// 时间选择器
- (void)setBirthdayFieldKeyboard: (NSInteger)tag
{
    
    dateView = [[UIView alloc]initWithFrame:CGRectMake(0, 470, self.view.frame.size.width, 330)];
    dateView.backgroundColor = [UIColor redColor];
    datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, 300)];
    // 只显示时间
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:0];
    datePicker.tag = tag;
    // 显示中文
    //datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"en"];
    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh-Hans-CN"];
    
    [dateView addSubview:datePicker];
    [self createBtn];
    [self.view addSubview:dateView];
}




- (void)createBtn {
    NSArray *title = [NSArray arrayWithObjects:@"取消",@"确认", nil];
    for (int i = 0; i<2; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*100+100, 0, 100, 30);
        [btn setTitle:title[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100+i;
        [dateView addSubview:btn];
        
    }
    
    
    
}

//datePick按钮
- (void)btnAction:(UIButton *)sender {
    
    if (sender.tag == 100) {
        
        [dateView removeFromSuperview];
    }
    
    else{
        
        NSLog(@"%@", datePicker.date);
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        formatter.dateFormat = @"yyyy-MM-dd";
        if (datePicker.tag == 1001) {
            self.startLabel.text = [formatter stringFromDate:datePicker.date];
        } else if (datePicker.tag == 1002) {
            self.endLabel.text = [formatter stringFromDate:datePicker.date];
        }
        [dateView removeFromSuperview];
    }
    
    
}

#pragma mark --- pickerView代理方法
// 显示的列数 必须的代理
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}




- (void)fenchDataSource {
    
    NSString *fromStr = [self.startLabel.text stringByAppendingString:@" 00:00:00"];
    NSString *toStr = [self.endLabel.text stringByAppendingString:@" 23:59:59"];

    //POST请求
    NSDictionary *mdic = [NSDictionary dictionaryWithObjectsAndKeys:UID,@"uid",fromStr,@"from",toStr,@"to",nil];
    [JNTDataRequestTools getFetchDataWithUrlString:Recharge_URL paramenters:mdic
                                           success:^(id data) {
                                               // 请求成功，解析数据
                                               NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
                                               NSLog(@"dic===%@",resultDic);
                                               NSArray *result = resultDic[@"result"];
                                               
                                               
                                           } fail:^{
                                               
                                               NSLog(@"话题列表请求失败");
                                           } view:self.view];
    
}


#pragma mark ---tableViewDelegate--

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 50;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RechargeDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    //cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.text = @"123";
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

#pragma mark ---按钮回调--

//起始日期
- (IBAction)startTap:(UITapGestureRecognizer *)sender {
    NSLog(@"起始日期");
    [self setBirthdayFieldKeyboard:sender.view.tag];
    
}

//截止日期
- (IBAction)endTap:(UITapGestureRecognizer *)sender {
    
    NSLog(@"截止日期");
    [self setBirthdayFieldKeyboard:sender.view.tag];
}


//查询
- (IBAction)queryBtn:(UIButton *)sender {
    
    NSLog(@"查询");
    [self fenchDataSource];

    
}



#pragma mark - 设置导航条
- (void)setUpNavgationBar
{
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"上栏"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.title = @"一卡通";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}


#pragma mark ---视图初始化--
-(UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:kRect(0, 170, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1.0];
        [_tableView registerNib:[UINib nibWithNibName:@"RechargeDetailsCell" bundle:nil] forCellReuseIdentifier:@"CELL"];
    }
    return _tableView;
}

@end
